const app = getApp();
var url = app.getURL();

Page({
  data: {
    listdata:[],
    good:false
  },

  //加载初始数据
  onLoad: function (options) {
    this.loadmore();
  },

  //触底事件
  onReachBottom:function(){
  },

  //发起请求
  loadmore:function(){
    var that = this;
    //请求服务器
    wx.request({
      url:url+'/app/comm/topic/queryList',
      method: "GET",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          listdata: res.data.data
        })
      }
    })
  },
 // 点赞
 good(e){
  console.log(e)
  let that = this
  let lisiId=e.currentTarget.dataset
  let id = wx.getStorageSync('message').id;
  let count = this.data.count;
  wx.request({
    url:url+'/app/comm/topic/auth/likeTopic',
    method: "POST",
    header: {
      'content-type': 'application/json',
      token:wx.getStorageSync('cookieToken')
    },
    data:{
      "commType": 1200,
      "likeContentId": lisiId.id,
      "likedAuthorId": lisiId.authorid,
      "postLikeId": id
    },
    success(res) {
      console.log(res)
      if(res.data.code==200){
        wx.showToast({
          title: '点赞成功',
          duration: 600,
          icon: 'success',
        })
        that.setData({
          good: true,
          count: count + 1
        })
      }else{
        wx.showToast({
          title: '点赞失败',
          duration: 600,
          icon: 'error',
        })
      }
    }
  })
},
  // 取消点赞
  unGood(e){
    let that = this
    let lisiId=e.currentTarget.dataset
    let id = wx.getStorageSync('message').id;
    let count = this.data.count;
    wx.request({
      url:url+'/app/comm/topic/auth/unLikeTopic',
      method: "POST",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "commType": 0,
        "likeContentId": lisiId.id,
        "likedAuthorId": lisiId.authorid,
        "postLikeId": id
      },
      success(res) {
        if(res.data.code==200){
          wx.showToast({
            title: '取消点赞成功',
            duration: 600,
            icon: 'success',
          })
          that.setData({
            good: false,
            count: count - 1
          })
        }else{
          wx.showToast({
            title: '取消点赞失败',
            duration: 600,
            icon: 'error',
          })
        }
      }
    })
  },  
  // 返回顶部
  onPageScroll: function (e) {
    // console.log(e.scrollTop)
    if (e.scrollTop>300) {
      this.setData({
        showTop: true
      })
    } else {
      this.setData({
        showTop: false
      })
    }
  },
  goTop: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },

  // 发布页面
  publish:function() {
    wx.navigateTo({
      url: '/pages/main/publish/publish?type='+2,
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button'){
       return
    }
  }
})
